package com.zhj.demo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.zhj.demo.base.AppUtils;
import com.zhj.demo.base.BolckChainItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class RecyclerTestActivity extends Activity {
    RecyclerView recyclerView;

    List<PersonBean> mDatas = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_layout);
        recyclerView = findViewById(R.id.recycler_view);

        for (int i = 0; i < 8; i++) {
            mDatas.add(new PersonBean("张三" + i, "北京长安大街" + i + "号"));
        }


        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new PersonAdapter(this));


        recyclerView.setLayoutManager(new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int pos = parent.getChildAdapterPosition(view);
                switch (pos){
                    case 0:
                        outRect.left = AppUtils.dp2(20);
                        outRect.top = AppUtils.dp2(20);
                        outRect.right = AppUtils.dp2(10);
                        outRect.bottom = AppUtils.dp2(10);
                        break;
                    case 1:
                    case 2:
                        outRect.left = AppUtils.dp2(10);
                        outRect.top = AppUtils.dp2(20);
                        outRect.right = AppUtils.dp2(10);
                        outRect.bottom = AppUtils.dp2(10);
                        break;
                    case 3:
                        outRect.left = AppUtils.dp2(10);
                        outRect.top = AppUtils.dp2(20);
                        outRect.right = AppUtils.dp2(20);
                        outRect.bottom = AppUtils.dp2(10);
                        break;

                    case 4:
                        outRect.left = AppUtils.dp2(20);
                        outRect.top = AppUtils.dp2(10);
                        outRect.right = AppUtils.dp2(10);
                        outRect.bottom = AppUtils.dp2(20);
                        break;
                    case 5:
                    case 6:
                        outRect.left = AppUtils.dp2(10);
                        outRect.top = AppUtils.dp2(10);
                        outRect.right = AppUtils.dp2(10);
                        outRect.bottom = AppUtils.dp2(20);
                        break;
                    case 7:
                        outRect.left = AppUtils.dp2(10);
                        outRect.top = AppUtils.dp2(10);
                        outRect.right = AppUtils.dp2(20);
                        outRect.bottom = AppUtils.dp2(20);
                        break;
                }
            }
        });
    }


    class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonHolder> {
        Context mContext;

        public PersonAdapter(Context context) {
            mContext = context;
        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }

        @NonNull
        @Override
        public PersonHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.person_list_item, parent, false);
            return new PersonHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull PersonHolder holder, int position) {
            holder.btnName.setText(mDatas.get(position).name);
        }


        public class PersonHolder extends RecyclerView.ViewHolder {
            public TextView btnName;

            public PersonHolder(View itemView) {
                super(itemView);
                btnName = itemView.findViewById(R.id.btn_name);

                // 340  195
                ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
                layoutParams.width = (AppUtils.dp2(340) - AppUtils.dp2(100)) / 4;
                layoutParams.height = (AppUtils.dp2(196) - AppUtils.dp2(60)) / 2;
                itemView.setLayoutParams(layoutParams);
            }
        }

    }


}
